package net.wanho.zaixian.course.service.impl;

import net.wanho.zaixian.course.dao.TeachPlanDao;
import net.wanho.zaixian.course.service.TeachPlanService;
import net.wanho.zaixian.pojo.course.Teachplan;
import net.wanho.zaixian.pojo.course.ext.TeachplanNode;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class TeachPlanServiceImpl implements TeachPlanService {

    @Resource
    private TeachPlanDao teachPlanDao;

    /**
     * 根据课程Id查询所有的课程计划
     * @param courseId
     * @return
     */
    @Override
    public List<TeachplanNode> getAllTeachPlanByCourseId(String courseId) {
        Teachplan teachplan = new Teachplan();
        teachplan.setCourseid(courseId);
        Example<Teachplan> example = Example.of(teachplan);
        List<Teachplan> teachplanList = teachPlanDao.findAll(example);
        List<TeachplanNode> teachplanNodeList = changeTeachePlanToTeacherplanNode(teachplanList);

        List<TeachplanNode> rootTeachPlan = getRootTeachPlan(teachplanNodeList);

        return rootTeachPlan;
    }

    public List<TeachplanNode> changeTeachePlanToTeacherplanNode(List<Teachplan> list){
        List<TeachplanNode> teachplanNodeList = new ArrayList<>();
        for(Teachplan teachplan:list){
            TeachplanNode teachplanNode = new TeachplanNode();
            teachplanNode.setId(teachplan.getId());
            teachplanNode.setPname(teachplan.getPname());
            teachplanNode.setParentid(teachplan.getParentid());
            teachplanNode.setGrade(teachplan.getGrade());
            teachplanNode.setPtype(teachplan.getPtype());
            teachplanNode.setDescription(teachplan.getDescription());
            teachplanNode.setTimelength(teachplan.getTimelength());
            teachplanNode.setCourseid(teachplan.getCourseid());
            teachplanNode.setOrderby(teachplan.getOrderby());
            teachplanNode.setStatus(teachplan.getStatus());
            teachplanNodeList.add(teachplanNode);
        }
        return teachplanNodeList;
    }

    /**
     * 根据所有的List<TeachplanNode>查找出tree型结构
     * @param list
     * @return
     */
    private List<TeachplanNode> getRootTeachPlan(List<TeachplanNode> list){

        List<TeachplanNode> teachplanNodeList = new ArrayList<>();
        for(TeachplanNode teachplanNode:list){
            //根据长度判断是否为根节点，如果是根节点，设置children;
            if(teachplanNode.getParentid().equals("0")){
                System.out.println(teachplanNode.getId());
                teachplanNode.setChildren(getChildrenTeachPlanNodeList(teachplanNode.getId(),list));
                teachplanNodeList.add(teachplanNode);
            }
        }
        return teachplanNodeList;

    }

    /**
     * 为teachplanNode设置children
     * @param pid 初始参数父id==0的id，和List<TeachplanNode>
     * @param list
     * @return
     */
    private List<TeachplanNode> getChildrenTeachPlanNodeList(String pid,List<TeachplanNode> list){
        List<TeachplanNode> list1 = new ArrayList<>();
        //遍历传来的List<TeachplanNode>
        for(TeachplanNode teachplanNode:list){
           //如果teachplanNode的parentId ==父亲的id
            if(teachplanNode.getParentid().equals(pid)){
                //将这个teachplanNode加入到集合中
                list1.add(teachplanNode);
                //递归调用这个方法使用这个teachplanNode作为根节点，查询它的子节点
                teachplanNode.setChildren(getChildrenTeachPlanNodeList(teachplanNode.getId(),list));
            }
        }
        if(list1.size()==0){
            list1=null;
        }
        return list1;
    }

    /**
     * 添加一个课程
     * @param teachplan
     */
    @Override
    public void addTeachList(Teachplan teachplan) {
        teachPlanDao.save(teachplan);
    }

    /**
     * 根据课程计划id查询课程
     * @param id
     * @return
     */
    @Override
    public Teachplan getTeachPlanById(String id) {
        Optional<Teachplan> optional = teachPlanDao.findById(id);
        if(optional !=null && optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    /**
     * 根据课程计划id删除课程计划
     * @param id
     */
    @Override
    public void deleteTeachPlanById(String id) {
        teachPlanDao.deleteById(id);

    }
}
